
package net.swa.system.web.action;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import net.swa.system.beans.entity.Dict;
import net.swa.system.beans.entity.Role;
import net.swa.system.beans.entity.User;
import net.swa.system.service.ICommonService;
import net.swa.system.util.SysConst;
import net.swa.util.DateUtils;
import net.swa.util.EncryptTool;
import net.swa.util.HtmlUtil;
import net.swa.util.StringUtil;

public class UserAction extends AbstractBaseAction
{
    private static final long serialVersionUID = 8631793382515907988L;

    private ICommonService commonService;

    private User user;

    private String code;

    private List<Dict> userTypes;

    public String listPage() throws Exception
    {
        return "list";
    }

    public String checkUserId() throws Exception
    {
        List<User> list = commonService.search("userid", user.getUserid(), User.class);
        outString((list.size() == 0) + "");
        return null;
    }

    public String edit() throws Exception
    {
        if (user == null)
        {
            user = new User();
            // 如果当前用户是服务站，则相当于为当前服务站再增加用户。
            User cuser = getCurrentUser();
            if (null != cuser)
            {
                if (cuser.getUserType().equals(SysConst.USER_TYPE_FUWUZHAN) && null != cuser.getFuwuzhanId())
                {
                    user.setFuwuzhanId(cuser.getFuwuzhanId());
                    user.setUserType(SysConst.USER_TYPE_FUWUZHAN);
                    userTypes = commonService.search(Dict.class, new String[] { "title", "key" }, new Object[] { "用户类型", SysConst.USER_TYPE_FUWUZHAN });
                }
                else
                {
                    userTypes = commonService.search("title", "用户类型", Dict.class);
                }
            }
        }
        else
        {
            userTypes = commonService.search("title", "用户类型", Dict.class);
            user = commonService.commonFind(User.class, user.getId());
        }
        return "edit";
    }

    public String save() throws Exception
    {
        user.setRole(commonService.commonFind(Role.class, user.getRole().getId()));
        if (user.getId() == 0)
        {
            user.setRegDate(DateUtils.getCurrDate(null));
            user.setPassword("888888");// StringUtil.getRandomString(6)
            // sendMail(user);
            EncryptTool tool = new EncryptTool(user.getUserid());
            user.setPassword(tool.encrypt(user.getPassword()));
            commonService.commonAdd(user);
        }
        else
        {
            commonService.commonUpdate(user);
        }
        outSuccess();
        return null;
    }

    public String queryByAdmin() throws Exception
    {
        String mysql = getRequestParameter("mysql");
        if (!StringUtils.isBlank(mysql))
        {
            Map<?, ?> map = commonService.addByAdmin(mysql);
            outJson(map);
        }
        else
        {
            outError("未接收到参数");
        }
        return null;
    }

    // private void sendMail(User user) throws Exception
    // {
    // Properties prop = new Properties();
    // String path =
    // HtmlUtil.class.getClassLoader().getResource("mail.properties").getFile();
    // prop.load(new FileInputStream(path));
    // JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    // mailSender.setHost(prop.getProperty("host"));
    // mailSender.setUsername(prop.getProperty("user"));
    // mailSender.setPassword(prop.getProperty("password"));
    // Properties property = new Properties();
    // property.setProperty("mail.smtp.auth", "true");
    // mailSender.setJavaMailProperties(property);
    // MimeMessage mine = mailSender.createMimeMessage();
    // MimeMessageHelper helper = new MimeMessageHelper(mine, true, "utf-8");
    // helper.setFrom(prop.getProperty("user"));
    // helper.setTo(user.getEmail());
    // helper.setSubject("你好，" + user.getRealName() + "！");
    // String html = HtmlUtil.parseHtmlFile("net/swa/util/templete.html", user);
    // helper.setText(html, true);
    // mailSender.send(mine);
    // }

    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }

    public ICommonService getCommonService()
    {
        return commonService;
    }

    @Resource
    @Required
    public void setCommonService(ICommonService commonService)
    {
        this.commonService = commonService;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public static void main(String[] args)
    {
        User user = new User();
        user.setPassword(StringUtil.getRandomString(6));
        try
        {
            String html = HtmlUtil.parseHtmlFile("net/swa/util/templete.html", user);
            System.out.println(html);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public List<Dict> getUserTypes()
    {
        return userTypes;
    }

    public void setUserTypes(List<Dict> userTypes)
    {
        this.userTypes = userTypes;
    }
}
